<style lang="less">
    @import "../../../styles/table.less";
</style>
<template>
    <div>
        <Drawer :closable="false" placement="right" v-model="drawerVisible" width="60%" :title="drawerTitle">
            <Row>
                <Col>
                    <Card>
                        <Row class="operation">
                            <Button @click="add" type="primary" icon="md-add">新建价格配置</Button>
                            <circleLoading v-if="operationLoading"/>
                        </Row>
                        <Row style="padding-top: 5px">
                            <Table :loading="loading" border :columns="columns" :data="data" sortable="custom" ref="table"></Table>
                        </Row>
                    </Card>
                </Col>
            </Row>
        </Drawer>
        <Modal :title="modalTitle" v-model="modalVisible" :mask-closable='false' :width="500">
            <Form ref="priceForm" :model="form" :label-width="80" :rules="formValidate">
                <FormItem label="价格名称" prop="priceName">
                    <Input v-model="form.priceName"/>
                </FormItem>
                <FormItem label="安卓原价" prop="androidOldPrice">
                    <Input v-model="form.androidOldPrice"/>
                </FormItem>
                <FormItem label="安卓现价" prop="androidPrice">
                    <Input v-model="form.androidPrice"/>
                </FormItem>
                <FormItem label="苹果原价" prop="iosOldPrice">
                    <Input v-model="form.iosOldPrice"/>
                </FormItem>
                <FormItem label="苹果现价" prop="iosPrice">
                    <Input v-model="form.iosPrice"/>
                </FormItem>
                <FormItem label="排序号" prop="sortOrder">
                    <Input v-model="form.sortOrder" placeholder="自定义排序号"/>
                </FormItem>
                <FormItem label="赠送积分" prop="scoreVip">
                    <Input v-model="form.scoreVip" :max="10" type="number" placeholder="购买vip赠送积分"/>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancel">取消</Button>
                <Button type="primary" :loading="submitLoading" @click="submitVipPrice">提交</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import circleLoading from "../../my-components/circle-loading.vue";
    import {add, update, list, remove} from "@/api/vipPrice";

    export default {
        name: "vipPriceDrawer",
        components: {
            circleLoading
        },
        data() {
            return {
                loading: false, // 表单加载状态
                operationLoading: false,
                drop: false,
                selectCount: 0, // 多选计数
                selectList: [], // 多选数据
                dropDownContent: "展开",
                drawerTitle: "价格配置",
                submitLoading: false,
                modalTitle: "添加价格配置",
                drawerVisible: false,
                modalVisible: false,
                dropDownIcon: "ios-arrow-down",
                form: {
                    id: "",
                    vipTypeId: "",
                    priceName: "",
                    androidPrice: 0,
                    androidOldPrice: 0,
                    iosPrice: 0,
                    sortOrder:0,
                    iosOldPrice: 0,
                    scoreVip: 0
                },
                formValidate: {
                    priceName: [{required: true, message: "名称不能为空", trigger: "blur"}]
                },
                selectDate: null, //选择日期绑定modal
                columns: [
                    /*{
                        type: "selection",
                        width: 60,
                        align: "center"
                    },*/
                    {
                        type: "index",
                        title: "序号",
                        width: 80,
                        align: "center"
                    },
                    {
                        title: "价格名称",
                        key: "priceName",
                        align: "center",
                        width: 150,
                        sortable: true
                    },
                    {
                        title: "安卓原价",
                        key: "androidOldPrice",
                        align: "center",
                        width: 120,
                        sortable: true
                    }, {
                        title: "安卓现价",
                        key: "androidPrice",
                        align: "center",
                        width: 120,
                        sortable: true
                    }, {
                        title: "苹果原价",
                        key: "iosOldPrice",
                        align: "center",
                        width: 120,
                        sortable: true
                    }, {
                        title: "苹果现价",
                        key: "iosPrice",
                        align: "center",
                        width: 120,
                        sortable: true
                    }, {
                        title: "排序号",
                        key: "sortOrder",
                        align: "center",
                        width: 120,
                        sortable: true
                    }, {
                        title: "赠送积分",
                        key: "scoreVip",
                        align: "center",
                        width: 120,
                        sortable: true
                    }, {
                        title: "操作",
                        key: "action",
                        width: 200,
                        align: "center",
                        fixed: "right",
                        render: (h, params) => {
                            return h("div", [
                                h(
                                    "Button",
                                    {
                                        props: {
                                            size: "small"
                                        },
                                        style: {
                                            marginRight: "5px"
                                        },
                                        on: {
                                            click: () => {
                                                this.edit(params.row);
                                            }
                                        }
                                    },
                                    "编辑"
                                ),
                                h(
                                    "Button",
                                    {
                                        props: {
                                            type: "error",
                                            size: "small"
                                        },
                                        on: {
                                            click: () => {
                                                this.remove(params.row);
                                            }
                                        }
                                    },
                                    "删除"
                                )
                            ]);
                        }
                    }],
                data: [], // 表单数据
                total: 0 // 表单数据总数
            }
        },
        methods: {
            init() {

            },
            getList() {
                this.loading = true;
                list(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.status === true) {
                        this.data = res.data.rows;
                    }
                });
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },
            showDrawer(params) {
                if (params.vipTypeId) {
                    this.type = 1;
                    this.drawerTitle = "价格设置";
                    this.form.vipTypeId = params.vipTypeId;
                    this.getList();
                }
                this.drawerVisible = true;
            },
            add() {
                this.modalType = 0;
                this.modalVisible = true;
            },
            edit(row) {
                this.modalType = 1;
                this.form = row;
                this.modalVisible = true;
            },
            setPrice(row) {
                let query = {type: 1, id: row.id};
                this.$refs.vipPriceDrawer.showDrawer(query);
            },
            remove(row) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除?",
                    onOk: () => {
                        remove(row.id).then(res => {
                            if (res.status === true) {
                                this.$Message.success("删除成功");
                                this.getList();
                            }
                        });
                    }

                });
            },
            cancel() {
                this.modalVisible = false;
            },
            submitVipPrice() {
                this.$refs.priceForm.validate(valid => {
                    if (valid) {
                        if(this.form.scoreVip < 0){
                            this.$Message.error("积分不能小于零");
                            return false;
                        }
                        if (this.modalType === 0) {
                            // 添加
                            this.submitLoading = true;
                            add(this.form).then(res => {
                                this.submitLoading = false;
                                if (res.status === true) {
                                    this.$Message.success("操作成功");
                                    this.getList();
                                    this.modalVisible = false;
                                }
                            });
                        } else {
                            this.submitLoading = true;
                            update(this.form).then(res => {
                                this.submitLoading = false;
                                if (res.status === true) {
                                    this.$Message.success("操作成功");
                                    this.getList();
                                    this.modalVisible = false;
                                }
                            });
                        }
                    }
                });
            },
            delAll() {

            }
        },
        mounted() {
            this.init();
        }
    }
</script>
